package com.warehouse.controller;

import com.warehouse.dto.PageResult;
import com.warehouse.dto.Result;
import com.warehouse.dto.ResultCode;
import com.warehouse.entity.Warehouse;
import com.warehouse.service.Wareservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @program: warehouse
 * @description: 仓库管理
 * @author: xiaojiu
 * @create: 2021-01-14 08:57
 */
@RestController
@Api(tags = "仓库管理xiaoxiao")
@RequestMapping("/api")
public class WarehouseController {
    /**
     * 分页查询所有
     */
    @Autowired(required = false)
    private Wareservice wareservice;

    @GetMapping("/findAllWarehouse")
    @ApiOperation(value = "分页查询所有仓库", httpMethod = "GET")
    public Result<PageResult<Warehouse>> findAllWarehouse(@RequestParam(required = false) Integer currentPage, @RequestParam(required = false) Integer pageSize) {
        PageResult<Warehouse> warehouses = wareservice.findAllWarehouse(currentPage, pageSize);
        return new Result<>(ResultCode.SUCCESS, warehouses);
    }

    /**
     * 添加仓库信息
     */
    @PostMapping("/addWarehouse")
    @ApiOperation(value = "添加仓库信息", httpMethod = "POST")
    public Result addWarehouse(@RequestBody Warehouse warehouse) {
        boolean w = wareservice.addWarehouse(warehouse);
        if (w) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 修改仓库信息
     */
    @PostMapping("/editWarehouse")
    @ApiOperation(value = "修改仓库信息", httpMethod = "POST")
    public Result editWarehouse(@RequestBody Warehouse warehouse) {
        boolean w = wareservice.editWarehouse(warehouse);
        if (w) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 删除仓库信息
     */
    @RequestMapping(value = "/delWarehouse", method = RequestMethod.DELETE)
    @ApiOperation(value = "修改仓库信息", httpMethod = "DELETE")
    public Result delWarehouse(String id) {
        boolean w = wareservice.delWarehouse(id);
        if (w) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 条件查询
     */
    @RequestMapping(value = "/findWarehouse", method = RequestMethod.POST)
    @ApiOperation(value = "条件模糊查询", httpMethod = "POST")
    public Result findWarehouse(@RequestBody Warehouse warehouse) {
        List<Warehouse> warehouses = wareservice.findWarehouse(warehouse);
        if (warehouses != null) {
            return new Result(ResultCode.SUCCESS, warehouses);
        }
        return new Result(ResultCode.FAIL);
    }
}
